import Taro from '@tarojs/taro';
import Api from './Api';
import userStore from 'src/pages/User/userStore';

class LoginApi extends Api {

    constructor() {
        super();

        Taro.login({
            success: async (res) => {
                if (res.code) {
                    const result = await loginApi.wxLogin(res.code);
                    if (result.code == 200) {
                        Taro.setStorageSync('token', result.msg);
                        console.log('登录成功,code:', res.code, result.msg);
                        userStore.setIsLogin(true);
                    }
                } else {
                    console.error('登录失败！', res.errMsg);
                }
            },
        });
        
    }

    // 微信授权登录
    wxLogin(code: string) {
        return this._get('/wx/getToken', { code });
    }


    init() { }

}


const loginApi = new LoginApi();

export default loginApi;